<?php
/**
 * Created by PhpStorm.
 * User: linhui
 * Date: 2016/12/14
 * Time: 1:21
 */

namespace Admin\Model;


use Think\Model;

class MenuModel extends Model
{
    /**
     * 获取菜单列表
     * @param int $parent_id
     * @param bool $is_allow
     * @return mixed
     */
    public function getMenuList($parent_id = 0, $is_allow = false)
    {
        //条件
        $map = array(
            'parent_id'=>$parent_id,
        );
        //获取权限内的菜单
        if(!$is_allow){
            if(!$this->allow_menu_id){
                $user_info = getLoginInfo();
                $role_ids = join(',', $user_info['role_ids']);
                if($role_ids){
                    $sql = "SELECT a.menu_id, m.parent_id FROM __ROLE_AUTH__ ra 
                        INNER JOIN __AUTH__ a ON a.auth_id = ra.auth_id 
                        LEFT JOIN __MENU__ m ON m.menu_id = a.menu_id
                        WHERE ra.role_id IN ({$role_ids})";
                    $allow_menu = $this->query($sql);

                    $allow_menu_id = array();
                    foreach($allow_menu as $menu){
                        if(!in_array($menu['menu_id'], $allow_menu_id)){
                            $allow_menu_id[] = $menu['menu_id'];
                        }
                        if(!in_array($menu['parent_id'], $allow_menu_id)){
                            $allow_menu_id[] = $menu['parent_id'];
                        }
                    }
                    $this->allow_menu_id = $allow_menu_id;
                }
            }
            if(!$this->allow_menu_id){
                return false;
            }

            $map['menu_id'] = array('in', $this->allow_menu_id);
        }

        $list = $this->where($map)->order('sort, menu_id')->getField('menu_id, menu_name, url,icon, sort, status', true);
        foreach($list as &$tmp){
            $tmp['sub'] = $this->getMenuList($tmp['menu_id']);
        }
        return $list;
    }


    /**
     * 面包屑
     * @param $route
     * @return array
     */
    public function getBreadCrumbs($route)
    {
        $current_menu = $this->getRouteMenu($route);
        $breadCrumbs = array();
        if($current_menu){
            $breadCrumbs[] = $current_menu;
            $this->getParentMenusByBreadCrumbs($current_menu, $breadCrumbs);
        }
        $result = array();
        for($i = count($breadCrumbs); $i > 0 ; $i--){
            $result[] = $breadCrumbs[$i - 1];
        }

        return $result;
    }

    /**
     * 获取上级面包屑 递归
     * @param $menu
     * @param $breadCrumbs
     */
    public function getParentMenusByBreadCrumbs($menu, &$breadCrumbs){
        $parentMenu = $this->where(array('menu_id'=>$menu['parent_id']))->field('menu_id, parent_id, url, menu_name, icon')->find();
        if($parentMenu){
            $breadCrumbs[] = $parentMenu;
            if($parentMenu['parent_id']){
                $this->getParentMenusByBreadCrumbs($parentMenu, $breadCrumbs);
            }
        }
    }

    /**
     * 获取菜单列表按级排序
     */
    public function getMenuListOrderByParentId($parent_id = 0){
        $result = array();
        $top_menus = $this->where(array('parent_id'=>$parent_id))->order('sort')->select();
        $level = 1;
        foreach($top_menus as $menu){
            $menu['level'] = $level;
            $result[] = $menu;
            $tmp = $this->getMenuListOrderByParentId($menu['menu_id']);
            foreach($tmp as &$val){
                $val['level']++;
            }
            $result = array_merge($result, $tmp);
        }

        return $result;
    }

    /**
     * 获取当前菜单
     * @param string $route
     * @return bool
     */
    public function getRouteMenu($route = '')
    {
        $tmp = rsort(explode('/', $route));
        $action_name = $tmp[0] ? $tmp[0] : ACTION_NAME;
        $controller_name = $tmp[1] ? $tmp[1] : CONTROLLER_NAME;
        $module_name = $tmp[2] ? $tmp[2] : MODULE_NAME;
        $sql = "SELECT m.menu_id, m.menu_name, m.url, m.parent_id, m.icon FROM __MENU__ m 
                  INNER JOIN __AUTH__ a ON a.menu_id = m.menu_id AND module_name = '{$module_name}' AND controller_name = '{$controller_name}' AND action_name = '{$action_name}'";
        $menu = $this->query($sql);
        return $menu ? $menu[0] : false;
    }
}